package com.test.memory.leak;

import java.util.Random;

import org.apache.log4j.Logger;

public class ReportGen extends Thread {

	static final Logger log = Logger.getLogger(ReportGen.class);

	private BoSession bosess;

	private int job;

	private boolean done = true;

	public ReportGen(String id) {
		super("ReportGen-#" + id);
	}

	public void run() {

		while (true) {
			if (done) {
				try {
					log.info("No task to do, waiting for a new task...");
					synchronized (this) {
						wait();
					}
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
			done = false;
			try {
				doReportJob();
			} catch (Exception e) {
				e.printStackTrace();
			}
			done = true;
		}
	}

	private void doReportJob() {
		log.info("Thread is doing job... ");
		try {
			Thread.sleep(new Random().nextInt(10)*1000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		log.info("Job finished.");
	}

	public BoSession getBosess() {
		return bosess;
	}

	public void setBosess(BoSession bosess) {
		this.bosess = bosess;
	}

	public boolean isDone() {
		return this.done;
	}

	public int getJob() {
		return job;
	}

	public void setJob(int job) {
		this.job = job;
	}

}
